package com.raining.inningz.ui.play;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;

import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.raining.inningz.R;
import com.raining.inningz.baseclass.BaseFragment;
import com.raining.inningz.constants.IParamConstants;

public class PlayAddPlayerFragment extends BaseFragment implements
		OnClickListener {

	private LayoutInflater mInflater;
	private View view;

	private ImageView imgBatsman;
	private ImageView imgBowler;
	private ImageView imgCaptain;
	private ImageView imgWicketKeeper;
	private ImageView imgSubstitute;

	private RelativeLayout relBatsman;
	private RelativeLayout relBowler;
	private RelativeLayout relCaptain;
	private RelativeLayout relWicketKeeper;
	private RelativeLayout relSubstitute;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {

			matchID = getArguments().getString(IParamConstants.MATCH_ID);

			isEdit = getArguments().getBoolean(IParamConstants.IS_EDIT);
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		mInflater = inflater;

		view = inflater.inflate(R.layout.fragment_play_add_players, container,
				false);
		context = mInflater.getContext();

		initialiseUI();

		setListner();

		setData();

		return view;
	}

	private void initialiseUI() {

		setHeaderIntialize(view);
		setLeftReturn();
		setLeftTextSelector(resources.getColor(R.color.top_bar_Button),
				resources.getColor(R.color.top_bar_Button_press), "InningZ");
		setRightTextSelector(resources.getColor(R.color.top_bar_Button),
				resources.getColor(R.color.top_bar_Button_press), "Save");
		setHeaderTitle(resources.getString(R.string.about), Color.BLACK);

		relBatsman = (RelativeLayout) view.findViewById(R.id.relBatsman);
		relBowler = (RelativeLayout) view.findViewById(R.id.relBowler);
		relCaptain = (RelativeLayout) view.findViewById(R.id.relCaptain);
		relWicketKeeper = (RelativeLayout) view
				.findViewById(R.id.relWicketKeeper);
		relSubstitute = (RelativeLayout) view.findViewById(R.id.relSubstitute);

		imgBatsman = (ImageView) view.findViewById(R.id.imgBatsman);
		imgBowler = (ImageView) view.findViewById(R.id.imgBowler);
		imgCaptain = (ImageView) view.findViewById(R.id.imgCaptain);
		imgWicketKeeper = (ImageView) view.findViewById(R.id.imgWicketKeeper);
		imgSubstitute = (ImageView) view.findViewById(R.id.imgSubstitute);

	}

	private void setListner() {

		getBtnLeft().setOnClickListener(this);
		getBtnRight().setOnClickListener(this);
		relBatsman.setOnClickListener(this);
		relBowler.setOnClickListener(this);
		relCaptain.setOnClickListener(this);
		relWicketKeeper.setOnClickListener(this);
		relSubstitute.setOnClickListener(this);

	}

	private void setData() {

		relBatsman.setTag(false);

		relBowler.setTag(false);
		relCaptain.setTag(false);

		relWicketKeeper.setTag(false);
		relSubstitute.setTag(false);

		imgBatsman.setTag(false);
		imgBowler.setTag(false);
		imgCaptain.setTag(false);
		imgWicketKeeper.setTag(false);
		imgSubstitute.setTag(false);

	}

	private void setUIModulation(View view) {
		
		if((view== relBatsman) && (view.getTag().equals(false))){
			relBatsman.setTag(true);
		}
		else if((view== relBatsman) && (view.getTag().equals(true))){
			relBatsman.setTag(false);
		}
		
		
		if((view== relBowler) && (view.getTag().equals(false))){
			relBowler.setTag(true);
		}
		else if((view== relBowler) && (view.getTag().equals(true))){
			relBowler.setTag(false);
		}
		
		
		if((view== relCaptain) && (view.getTag().equals(false))){
			relCaptain.setTag(true);
			relSubstitute.setTag(false);
		}
		else if((view== relCaptain) && (view.getTag().equals(true))){
			relCaptain.setTag(false);
		}
		
		
		if((view== relWicketKeeper) && (view.getTag().equals(false))){
			relWicketKeeper.setTag(true);
			relSubstitute.setTag(false);
		}
		else if((view== relWicketKeeper) && (view.getTag().equals(true))){
			relWicketKeeper.setTag(false);
		}
		
		
		System.out.println("Cap=="+relCaptain.getTag());
		System.out.println("Wick =="+relWicketKeeper.getTag());
		System.out.println("View =="+view.getTag());
		
		if((view== relSubstitute) && (view.getTag().equals(false))&&(relCaptain.getTag().equals(false))&& (relWicketKeeper.getTag().equals(false))){
			
			relSubstitute.setTag(true);
		}
		else if((view== relSubstitute) && (view.getTag().equals(true))){
			relSubstitute.setTag(false);
		}
		
		
		
		
		
		
		if(relBatsman.getTag().equals(true)){
			imgBatsman.setVisibility(View.VISIBLE);
		}
		else if(relBatsman.getTag().equals(false)){
			imgBatsman.setVisibility(View.INVISIBLE);
		}
		
		

		if(relBowler.getTag().equals(true)){
			imgBowler.setVisibility(View.VISIBLE);
		}
		else if(relBowler.getTag().equals(false)){
			imgBowler.setVisibility(View.INVISIBLE);
		}
		

		if(relCaptain.getTag().equals(true)){
			imgCaptain.setVisibility(View.VISIBLE);
		}
		else if(relCaptain.getTag().equals(false)){
			imgCaptain.setVisibility(View.INVISIBLE);
		}
		

		if(relWicketKeeper.getTag().equals(true)){
			imgWicketKeeper.setVisibility(View.VISIBLE);
		}
		else if(relWicketKeeper.getTag().equals(false)){
			imgWicketKeeper.setVisibility(View.INVISIBLE);
		}
		

		if(relSubstitute.getTag().equals(true)){
			imgSubstitute.setVisibility(View.VISIBLE);
		}
		else if(relSubstitute.getTag().equals(false)){
			imgSubstitute.setVisibility(View.INVISIBLE);
		}
		
		
		
		
		
	}

	@Override
	public void onClick(View v) {

		if (v == getBtnLeft()) {
			fragmentManager.popBackStack();
		} else if (v == getBtnRight()) {

		} else if (v == relBatsman) {

			setUIModulation(relBatsman);
		} else if (v == relBowler) {
			setUIModulation(relBowler);
		} else if (v == relCaptain) {
			setUIModulation(relCaptain);
		} else if (v == relWicketKeeper) {
			setUIModulation(relWicketKeeper);
		} else if (v == relSubstitute) {
			setUIModulation(relSubstitute);
		}

	}

}
